package com.jsmtr.rms.controller;

import cn.ktjk.DataTableUtile.DataTable.DataRow;
import cn.ktjk.DataTableUtile.DataTable.DataTable;
import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.*;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.ExcelHelper;
import com.jsmtr.rms.utils.ExcelImportHelper.dao.JDBCHelper;
import com.jsmtr.rms.utils.ExcelImportHelper.service.IImportResultService;
import com.jsmtr.rms.utils.ExcelImportHelper.service.impl.ImportResultService;
import com.jsmtr.rms.utils.ExcelImportHelper.service.impl.ImportZZReusltService;
import com.jsmtr.rms.utils.ResultBean;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2018/11/20 10:06
 * @modified by:
 */
@RequestMapping("/ExcelImport")
@Controller
public class ExcelImportController {
    @Resource
    private IUtilService utilService;
    @Resource
    private IDoMSService msService;
    @Resource
    private IProjectService projectService;
    @Resource
    private INZService nzService;
    @Resource
    private IDoSZService szService;
    @Resource
    private IRecommendService recommendService;

    @Resource
    private IDoResumeService doResumeService;


    @RequestMapping("/ImportExcelPage")
    public String ImportExcelPage(HttpServletRequest request, HttpServletResponse response, Model model){
        Map parameterMap=request.getParameterMap();
        StringBuffer searchedata=new StringBuffer();

        for (Object pkey : parameterMap.keySet()) {
            String value=((String[])parameterMap.get(pkey))[0];
            try {
                searchedata.append("<input type=\"hidden\" name=\""+pkey.toString()+"\" value=\""+ URLDecoder.decode(value,"UTF-8")+"\"/>");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        model.addAttribute("searchdata",searchedata.toString());
        return "/ExcelDown/ExcelImportPage";
    }

    @ResponseBody
    @RequestMapping("/getProcess")
    public ResultBean getProcess(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
       //Object excel_ps= session.getAttribute("excel_ps");//
       Object excel_totalNum=session.getAttribute("excel_totalNum");
       Object excel_errorNum=session.getAttribute("excel_errorNum");
       Object excel_successNum=session.getAttribute("excel_successNum");
       ResultBean resultBean=new ResultBean();
       if (excel_totalNum!=null&&excel_errorNum!=null&&excel_successNum!=null){
           HashMap datas=new HashMap();
           Integer totalNum=Integer.parseInt(excel_totalNum.toString());
           Integer successNum=Integer.parseInt(excel_successNum.toString());
           Integer errorNum=Integer.parseInt(excel_errorNum.toString());
           datas.put("totalNum",totalNum);
           datas.put("successNum",successNum);
           datas.put("errorNum",errorNum);

            resultBean.setState(true);
            resultBean.setDatas(datas);
       }else{
           resultBean.setState(false);
           resultBean.setMessage("无法读取导入进度信息！");
       }
       return resultBean;
    }


    @ResponseBody
    @RequestMapping(value = {"/ImportExcel"}, method = RequestMethod.POST)
    public void ImportExcel(@RequestParam("excel") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        DataTable dataTable=null;
        org.apache.poi.ss.usermodel.Workbook workbook;
     //   ResultBean resultBean=new ResultBean();
        HttpSession session=request.getSession();
        Integer excel_totalNum=0;
        Integer excel_errorNum=0;
        Integer excel_successNum=0;

        try {
            String pid=request.getParameter("pid").trim();//招聘方案
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            BaseUser baseUser = utilService.getSessionUser(request);

            //region 清空session中Excel相关数量
            session.removeAttribute("excel_totalNum");
            session.removeAttribute("excel_errorNum");
            session.removeAttribute("excel_successNum");
            //endregion

            //判断操作用户为否为责任部门
            if (baseUser.getUdept().equals(deptcode)) {
                ProjectInfoFlow projectInfoFlow=utilService.getFlow(fid);
                if (projectInfoFlow.getState() == EnumTools.PlanFlowState.进行中.ordinal()) {
                    //Workbook rwb = Workbook.getWorkbook(file.getInputStream());

                    try{
                        //Excel转换为DataTable
                        dataTable= ExcelHelper.ReadExcelToDataTable(file);
                    }catch (Exception e){
                        utilService.printJsonResult(false, "只能上传后缀为：xls和xlsx的Excel文件", "no", response);
                    }

                    if (dataTable!=null&&dataTable.Rows.size() > 0) {
                        Integer cardNoIndex=null;
                        Integer sorceIndex=null;
                        Integer resultIndex=null;
                        Integer rulesIndex=null;
                        excel_totalNum=dataTable.Rows.size();
                        session.setAttribute("excel_totalNum",excel_totalNum);
                        for (int i=0;i<dataTable.Columns.size();i++){
                            String columnName=dataTable.Columns.get(i).ColumnName;
                            if(columnName.indexOf("身份证")>=0){
                                cardNoIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("成绩")>=0){
                                sorceIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("结果")>=0){
                                resultIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("规则")>=0){
                                rulesIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("备注")>=0){
                                rulesIndex=i;
                                continue;
                            }
                        }
                        if(cardNoIndex==null){
                            utilService.printJsonResult(false, "无法识别身份证列！", "no", response);
                        }
                        if(sorceIndex==null&&resultIndex==null){
                            utilService.printJsonResult(false, "无法识别成绩或者结果列！", "no", response);
                        }

                        for (DataRow row : dataTable.Rows) {
                            String cardNo="";
                            if(cardNoIndex!=null){
                                cardNo=row.getValue(cardNoIndex).toString().toUpperCase();
                            }
                            String sorce="";
                            if(sorceIndex!=null){
                                sorce = row.getValue(sorceIndex).toString();
                            }
                            String msresult="";
                            if(resultIndex!=null){
                                msresult = row.getValue(resultIndex).toString();
                            }
                            String rulesresult=null;
                            if(rulesIndex!=null){
                                rulesresult = row.getValue(rulesIndex).toString();
                            }

                            if(cardNo!=null&&!cardNo.equalsIgnoreCase("")){
                                if(!pid.startsWith("C_")){
                                    List<DUserResume> userResumeList=doResumeService.getDResumeByCardNo(cardNo,pid);
                                    if (userResumeList.size()==0){
                                        row.setValue(dataTable.Columns.size()-1,"身份证不存在!");
                                        excel_errorNum++;
                                        continue;
                                    }
                                    if(userResumeList.size()==1){
                                        IImportResultService importResultService=new ImportResultService();
                                        DUserResume dUserResume=userResumeList.get(0);
                                        Integer importResult=-1;
                                        try {
                                            if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.简历筛选.name())){
                                                importResult=  importResultService.saveResumeResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.素质.name())){
                                                importResult=  saveQAResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.笔试.name())){
                                                importResult=  importResultService.saveExamResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.背景调查.name())){
                                                importResult=  importResultService.saveReviewResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.面试.name())){
                                                importResult=  importResultService.saveInterviewResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.资料审核.name())){
                                                importResult=  importResultService.saveCheckResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.体检.name())){
                                                importResult=  importResultService.savePhysicalResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.实作.name())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.签约.name())){
                                                importResult=  importResultService.saveSigingResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);

                                            }else if(projectInfoFlow.getFid().equals(EnumTools.FlowNomalState.录用.name())){
                                                importResult=  importResultService.saveEmployResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }
                                            if (importResult>0){
                                                row.setValue(dataTable.Columns.size()-1,"已导入");
                                                excel_successNum++;
                                            }else{
                                                row.setValue(dataTable.Columns.size()-1,"未导入");
                                                excel_errorNum++;
                                            }

                                        }catch (Exception e){
                                            e.printStackTrace();
                                            row.setValue(dataTable.Columns.size()-1,"导入失败");
                                            excel_errorNum++;
                                        }
                                    }else{
                                        row.setValue(dataTable.Columns.size()-1,"存在多条数据");
                                        excel_errorNum++;
                                    }
                                }else{
                                    List<HRZZUser> hrzzUserList=recommendService.getUserList(pid,cardNo,deptcode);

                                    if(hrzzUserList==null||hrzzUserList.size()==0){
                                        row.setValue(dataTable.Columns.size()-1,"身份证不存在!");
                                        excel_errorNum++;
                                        continue;
                                    }

                                    if(hrzzUserList.size()>1){
                                        row.setValue(dataTable.Columns.size()-1,"存在多条数据!");
                                        excel_errorNum++;
                                        continue;
                                    }
                                    if(hrzzUserList.size()==1){
                                        IImportResultService importResultService=new ImportZZReusltService();
                                        HRZZUser   hrzzUser=hrzzUserList.get(0);
                                        Integer importResult=-1;
                                        try {
                                            if (projectInfoFlow.getNickname().equals("简历筛选")||projectInfoFlow.getNickname().equals("毕业生推荐")){
                                                importResult=  importResultService.saveResumeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.背景调查.name())){
                                                importResult=  importResultService.saveReviewResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.素质.name())){
                                                importResult=  saveQAResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.面试.name())){
                                                importResult=  importResultService.saveInterviewResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                          /*  }else if(projectInfoFlow.getFid().equals(EnumTools.ZZFlowState.资料审核.ordinal())){
                                                importResult=  importResultService.saveCheckResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);*/
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.体检.name())){
                                                importResult=  importResultService.savePhysicalResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                          /*  }else if(projectInfoFlow.getFid().equals(EnumTools.ZZFlowState.实作.ordinal())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);*/
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.实习.name())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);

                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.录用.name())){
                                                importResult=  importResultService.saveEmployResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.签约.name())) {
                                                importResult = importResultService.saveSigingResult(baseUser, pid, fid, hrzzUser.getId(), sorce, msresult, rulesresult);
                                            }
                                            if (importResult>0){
                                                row.setValue(dataTable.Columns.size()-1,"已导入");
                                                excel_successNum++;
                                            }else{
                                                row.setValue(dataTable.Columns.size()-1,"未导入");
                                                excel_errorNum++;
                                            }

                                        }catch (Exception e){
                                            e.printStackTrace();
                                            row.setValue(dataTable.Columns.size()-1,"导入失败");
                                            excel_errorNum++;
                                        }
                                    }

                                }

                            }else{
                                excel_errorNum++;
                                row.setValue(dataTable.Columns.size()-1,"无法识别身份证号!");
                            }
                            session.setAttribute("excel_totalNum",excel_totalNum);
                            session.setAttribute("excel_errorNum",excel_errorNum);
                            session.setAttribute("excel_successNum",excel_successNum);
                        }
                        org.apache.poi.ss.usermodel.Workbook reusultbook=ExcelHelper.DataTableToExcel(dataTable,projectInfoFlow.getNickname()+"导入结果");
                        try {
                            String fileName = file.getOriginalFilename();
                            String rfilename=fileName.substring(0,fileName.lastIndexOf(".")) + "导入结果.xls";
                            String uploadPath=request.getServletContext().getRealPath("\\resources\\upload");
                            String rfilepath=uploadPath+"\\excelresult";
                            File rdir=new File(rfilepath);

                            if(rdir.exists()) {
                                String[] filelist = rdir.list();
                                for (int i = 0; i < filelist.length; i++) {
                                    File readfile = new File(rfilepath + "\\" + filelist[i]);
                                    readfile.delete();
                                }
                            }else{
                                boolean bmkdir= rdir.mkdir();
                                if(!bmkdir){
                                    rdir.mkdirs();
                                }

                            }
                            File rfile=new File(rfilepath+"\\"+rfilename);
                            if(rfile.createNewFile()) {
                                FileOutputStream fileOutputStream = new FileOutputStream(rfile);
                                reusultbook.write(fileOutputStream);
                            }
                            utilService.printJsonResult(true, "操作成功！", "no", response);

                        }catch (Exception e){
                            e.printStackTrace();
                            utilService.printJsonResult(false, "操作失败！", "no", response);
                        }
                    }else{
                        utilService.printJsonResult(false, "读取Excel文件错误,未读取到导入数据！", "no", response);
                    }

                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            }else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        }catch(Exception e){
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    public Integer saveQAResult(BaseUser baseUser,String pid, String fid, String userID, String sorce, String result, String remark) throws Exception {
        DoSSUser doSSUser = doResumeService.getSsUser(fid,userID);
        List elist = new ArrayList();
        if(doSSUser == null){
            DoSSUser _user = new DoSSUser();
            _user.setId(UUID.randomUUID().toString());
            _user.setFid(fid);
            _user.setUserid(userID);
            _user.setPoint(sorce);
            _user.setResult(result);
            _user.setBz(remark);
            _user.setNotice(EnumTools.NoticeState.未发通知.ordinal());
            _user.setOp8("未公布");
            _user.setPointuser(baseUser.getUname());
            _user.setPointdate(new Date());
            elist.add(_user);
            utilService.addList(elist);
        }else {
            doSSUser.setPoint(sorce);
            doSSUser.setResult(result);
            doSSUser.setBz(remark);
            doSSUser.setPointuser(baseUser.getUname());
            doSSUser.setPointdate(new Date());
            elist.add(doSSUser);
            utilService.editList(elist);
        }

        return 1;
    }

    /*@ResponseBody
    @RequestMapping(value = {"/ImportExcel"}, method = RequestMethod.POST)
    public ResultBean ImportExcel(@RequestParam("excel") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        DataTable dataTable=null;
        org.apache.poi.ss.usermodel.Workbook workbook;
        ResultBean resultBean=new ResultBean();
        HttpSession session=request.getSession();
        Integer excel_totalNum=0;
        Integer excel_errorNum=0;
        Integer excel_successNum=0;

        try {
            String pid=request.getParameter("pid").trim();//招聘方案
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            BaseUser baseUser = utilService.getSessionUser(request);

            //region 清空session中Excel相关数量
            session.removeAttribute("excel_totalNum");
            session.removeAttribute("excel_errorNum");
            session.removeAttribute("excel_successNum");
            //endregion

            //判断操作用户为否为责任部门
            if (baseUser.getUdept().equals(deptcode)) {
               ProjectInfoFlow projectInfoFlow=utilService.getFlow(fid);
                if (projectInfoFlow.getState() == EnumTools.PlanFlowState.进行中.ordinal()) {
                    //Workbook rwb = Workbook.getWorkbook(file.getInputStream());

                    try{
                        //Excel转换为DataTable
                        dataTable= ExcelHelper.ReadExcelToDataTable(file);
                    }catch (Exception e){
                        resultBean.setState(false);
                        resultBean.setMessage("只能上传后缀为：xls和xlsx的Excel文件");
                        resultBean.setUrl("no");
                       return  resultBean;
                    }

                    if (dataTable!=null&&dataTable.Rows.size() > 0) {
                        Integer cardNoIndex=null;
                        Integer sorceIndex=null;
                        Integer resultIndex=null;
                        Integer rulesIndex=null;
                        excel_totalNum=dataTable.Rows.size();
                        session.setAttribute("excel_totalNum",excel_totalNum);
                        for (int i=0;i<dataTable.Columns.size();i++){
                            String columnName=dataTable.Columns.get(i).ColumnName;
                            if(columnName.indexOf("身份证")>=0){
                                cardNoIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("成绩")>=0){
                                sorceIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("结果")>=0){
                                resultIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("规则")>=0){
                                rulesIndex=i;
                                continue;
                            }
                            if(columnName.indexOf("备注")>=0){
                                rulesIndex=i;
                                continue;
                            }
                        }
                        if(cardNoIndex==null){
                            resultBean.setState(false);
                            resultBean.setMessage("无法识别<span style=color: red>身份证</span>列！");
                            resultBean.setUrl("no");
                            return  resultBean;
                        }
                        if(sorceIndex==null&&resultIndex==null){
                            resultBean.setState(false);
                            resultBean.setMessage("无法识别<span style=color: red>成绩</span>或者<span style=color: red>结果</span>列！");
                            resultBean.setUrl("no");
                            return  resultBean;
                        }

                        *//*if(rulesIndex==null){
                            resultBean.setState(false);
                            resultBean.setMessage("无法识别<span style=color: red>评分规则</span>或者<span style=color: red>备注</span>列！");
                            resultBean.setUrl("no");
                            return  resultBean;
                        }*//*
                       // List<DoMSUser> editlist = new ArrayList<DoMSUser>();
                       // List<HRZZUser> editZZlist = new ArrayList<HRZZUser>();
                        for (DataRow row : dataTable.Rows) {
                            String cardNo="";
                            if(cardNoIndex!=null){
                                cardNo=row.getValue(cardNoIndex).toString().toUpperCase();
                            }
                            String sorce="";
                            if(sorceIndex!=null){
                                sorce = row.getValue(sorceIndex).toString();
                            }
                            String msresult="";
                            if(resultIndex!=null){
                                msresult = row.getValue(resultIndex).toString();
                            }
                            String rulesresult=null;
                            if(rulesIndex!=null){
                                rulesresult = row.getValue(rulesIndex).toString();
                            }

                            if(cardNo!=null&&!cardNo.equalsIgnoreCase("")){
                                if(!pid.startsWith("C_")){
                                    List<DUserResume> userResumeList=doResumeService.getDResumeByCardNo(cardNo,pid);
                                    if (userResumeList.size()==0){
                                        row.setValue(dataTable.Columns.size()-1,"身份证不存在!");
                                        excel_errorNum++;
                                        continue;
                                    }
                                    if(userResumeList.size()==1){
                                        IImportResultService importResultService=new ImportResultService();
                                        DUserResume dUserResume=userResumeList.get(0);
                                        Integer importResult=-1;
                                        try {
                                            if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.简历筛选.name())){
                                                importResult=  importResultService.saveResumeResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.素质.name())){
                                                importResult=  saveQAResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.笔试.name())){
                                                importResult=  importResultService.saveExamResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.背景调查.name())){
                                                importResult=  importResultService.saveReviewResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.面试.name())){
                                                importResult=  importResultService.saveInterviewResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.资料审核.name())){
                                                importResult=  importResultService.saveCheckResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.体检.name())){
                                                importResult=  importResultService.savePhysicalResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.实作.name())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.签约.name())){
                                                importResult=  importResultService.saveSigingResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);

                                            }else if(projectInfoFlow.getFid().equals(EnumTools.FlowNomalState.录用.name())){
                                                importResult=  importResultService.saveEmployResult(baseUser,pid,fid,dUserResume.getId(),sorce,msresult,rulesresult);
                                            }
                                            if (importResult>0){
                                                row.setValue(dataTable.Columns.size()-1,"已导入");
                                                excel_successNum++;
                                            }else{
                                                row.setValue(dataTable.Columns.size()-1,"未导入");
                                                excel_errorNum++;
                                            }

                                        }catch (Exception e){
                                            e.printStackTrace();
                                            row.setValue(dataTable.Columns.size()-1,"导入失败");
                                            excel_errorNum++;
                                        }
                                    }else{
                                        row.setValue(dataTable.Columns.size()-1,"存在多条数据");
                                        excel_errorNum++;
                                    }
                                }else{
                                    List<HRZZUser> hrzzUserList=recommendService.getUserList(pid,cardNo,deptcode);

                                    if(hrzzUserList==null||hrzzUserList.size()==0){
                                        row.setValue(dataTable.Columns.size()-1,"身份证不存在!");
                                        excel_errorNum++;
                                        continue;
                                    }

                                    if(hrzzUserList.size()>1){
                                        row.setValue(dataTable.Columns.size()-1,"存在多条数据!");
                                        excel_errorNum++;
                                        continue;
                                    }
                                    if(hrzzUserList.size()==1){
                                        IImportResultService importResultService=new ImportZZReusltService();
                                        HRZZUser   hrzzUser=hrzzUserList.get(0);
                                        Integer importResult=-1;
                                        try {
                                           if (projectInfoFlow.getNickname().equals("简历筛选")||projectInfoFlow.getNickname().equals("毕业生推荐")){
                                                importResult=  importResultService.saveResumeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if (projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.背景调查.name())){
                                                importResult=  importResultService.saveReviewResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.面试.name())){
                                                importResult=  importResultService.saveInterviewResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                          *//*  }else if(projectInfoFlow.getFid().equals(EnumTools.ZZFlowState.资料审核.ordinal())){
                                                importResult=  importResultService.saveCheckResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);*//*
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.体检.name())){
                                                importResult=  importResultService.savePhysicalResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                          *//*  }else if(projectInfoFlow.getFid().equals(EnumTools.ZZFlowState.实作.ordinal())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);*//*
                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.实习.name())){
                                                importResult=  importResultService.savePracticeResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);

                                            }else if(projectInfoFlow.getNickname().equals(EnumTools.ZZFlowState.录用.name())){
                                                importResult=  importResultService.saveEmployResult(baseUser,pid,fid,hrzzUser.getId(),sorce,msresult,rulesresult);
                                              }else if(projectInfoFlow.getNickname().equals(EnumTools.FlowNomalState.签约.name())) {
                                                importResult = importResultService.saveSigingResult(baseUser, pid, fid, hrzzUser.getId(), sorce, msresult, rulesresult);
                                            }
                                            if (importResult>0){
                                                row.setValue(dataTable.Columns.size()-1,"已导入");
                                                excel_successNum++;
                                            }else{
                                                row.setValue(dataTable.Columns.size()-1,"未导入");
                                                excel_errorNum++;
                                            }

                                        }catch (Exception e){
                                            e.printStackTrace();
                                            row.setValue(dataTable.Columns.size()-1,"导入失败");
                                            excel_errorNum++;
                                        }
                                    }

                                }

                            }else{
                                excel_errorNum++;
                                row.setValue(dataTable.Columns.size()-1,"无法识别身份证号!");
                            }
                            session.setAttribute("excel_totalNum",excel_totalNum);
                            session.setAttribute("excel_errorNum",excel_errorNum);
                            session.setAttribute("excel_successNum",excel_successNum);
                        }
                        org.apache.poi.ss.usermodel.Workbook reusultbook=ExcelHelper.DataTableToExcel(dataTable,projectInfoFlow.getNickname()+"导入结果");
                        try {
                            String fileName = file.getOriginalFilename();
                            String rfilename=fileName.substring(0,fileName.lastIndexOf(".")) + "导入结果.xls";
                            String uploadPath=request.getServletContext().getRealPath("\\resources\\upload");
                            String rfilepath=uploadPath+"\\excelresult";
                            File rdir=new File(rfilepath);

                            if(rdir.exists()) {
                                String[] filelist = rdir.list();
                                for (int i = 0; i < filelist.length; i++) {
                                    File readfile = new File(rfilepath + "\\" + filelist[i]);
                                    readfile.delete();
                                }
                            }else{
                                boolean bmkdir= rdir.mkdir();
                                if(!bmkdir){
                                    rdir.mkdirs();
                                }

                            }
                            File rfile=new File(rfilepath+"\\"+rfilename);
                            if(rfile.createNewFile()) {
                                FileOutputStream fileOutputStream = new FileOutputStream(rfile);
                                reusultbook.write(fileOutputStream);
                                resultBean.setState(true);
                                resultBean.setMessage("操作成功！");
                                resultBean.setUrl(request.getContextPath()+"/resources/upload/excelresult/" + rfilename);
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                            resultBean.setState(false);
                            resultBean.setMessage("操作失败！");
                            resultBean.setUrl("no");
                        }
                    }else{
                        resultBean.setState(false);
                        resultBean.setMessage("读取Excel文件错误,未读取到导入数据！");
                        resultBean.setUrl("no");
                    }

                } else {
                    resultBean.setState(false);
                    resultBean.setMessage("当前流程不支持此操作！");
                    resultBean.setUrl("no");
                }
            }else {
                resultBean.setState(false);
                resultBean.setMessage("您无权执行此操作！");
                resultBean.setUrl("no");
            }
            return  resultBean;
        }catch(Exception e){
            e.printStackTrace();
            resultBean.setState(false);
            resultBean.setMessage("操作失败！\"");
            resultBean.setUrl("no");
        }
        return  resultBean;
    }*/

}
